<%@ page language="java" import="java.lang.*" import="java.sql.*" %>

<HTML>
    <BODY>
        <FORM NAME="f1" ACTION="EmployeeInformation.jsp" method="post">
            <%
                        Connection con = null;
                        String str = request.getParameter("r1");
                        String empname = request.getParameter("empname");
                        String code = request.getParameter("empid");
                        int ent = 0;
                        String failed = "";
                        try {
                            String click = "SELECT COUNT(*) FROM Employee WHERE empid='" + code + "' and empname='" + empname + "'";
                            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                            con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=JspJavaScript", "sa", "fat123456");
                            Statement stmt = null;
                            stmt = con.createStatement();
                            ResultSet ok = stmt.executeQuery(click);
                            while (ok.next()) {
                                ent = ok.getInt(1);
                            }
                            if (ent == 0) {
                                String insertQry = "insert Employee values('" + code + "','" + empname + "')";
                                int val = stmt.executeUpdate(insertQry);

            %>
            <script language="javascript">
                alert("Insertion successful");
                document.location="EmployeeInformation.jsp";
            </script>
            <%
                }
                if (ent == 1) {
            %>
            <script language="javascript">
                alert("This Emp ID already Exists");
                document.location="EmployeeInformation.jsp";
            </script>
            <%                }
                            stmt.close();
                            con.close();
                        } catch (Exception e) {
                            out.println(e.toString());
                        }
            %>
        </FORM>
    </BODY>
</HTML>
